package com.guli.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.eduservice.bean.EduVideo;
import com.guli.eduservice.client.VodClient;
import com.guli.eduservice.mapper.EduVideoMapper;
import com.guli.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-04
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Resource
    private VodClient vodClient;
    //根据课程id删除所有的小节并删除里面所有的视频
    @Override
    public boolean deletVideoByCourseId(String courseId) {
        //查出所有的视频id
        QueryWrapper<EduVideo> wrapper1 = new QueryWrapper<>();
        QueryWrapper<EduVideo> course_id = wrapper1.eq("course_id", courseId);
        wrapper1.select("video_source_id");
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper1);
        //类型转换List<EduVideo> ----> List<String>
        List<String> arrayList = new ArrayList<>();
        for (EduVideo eduVideo : eduVideos) {
            String videoId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoId)){
                arrayList.add(videoId);
            }

        }
        if(arrayList.size()>0){
            vodClient.removeVideos(arrayList); //删除所有的视频
        }

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        int delete = baseMapper.delete(wrapper);
        return delete > 0;
    }
}
